package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.CompanyServiceTypeMapper;
import com.hq.ims.data.entity.CompanyServiceType;
import com.hq.ims.data.entity.Dict;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.vo.CompanyServiceTypeMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.BooleanUtils;
import com.hq.utils.util.ObjectUtils;
import com.hq.utils.util.StringUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import lombok.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 企业服务类型 服务实现类
 * @since 20241022
 */
@Service
public class BaseCompanyServiceTypeService extends BaseService<CompanyServiceTypeMapper, CompanyServiceType> {

    @Resource
    private BaseDictService baseDictService;


    /**
     * 分页
     */
    public List<CompanyServiceTypeMapstruct.CompanyServiceTypeVo> getByPage(
            RequestPage page,
            CompanyServiceTypeMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<CompanyServiceType> lq = this.lambdaQuery();
        //查询条件

        //执行分页
        lq.page(page);

        List<CompanyServiceType> dataList = page.getRecords();
        List<CompanyServiceTypeMapstruct.CompanyServiceTypeVo> voList = CompanyServiceTypeMapstruct.INSTANCE.toCompanyServiceTypeVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    public CompanyServiceType addData(CompanyServiceTypeMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);
        Dict companyServiceTypeDict = baseDictService.getById(vo.getServiceTypeId());
        ObjectUtils.isNullThrow(companyServiceTypeDict, "产业标记不存在");

        boolean existsCompanyIdAndServiceTypeId = this.isExistsCompanyIdAndServiceTypeId(vo.getCompanyId(), vo.getServiceTypeId(), null);
        BooleanUtils.isTrueThrow(existsCompanyIdAndServiceTypeId, "该产业标记已经存在");

        //账值新的对象
        CompanyServiceType entity = new CompanyServiceType();
        entity.setCompanyId(vo.getCompanyId());
        entity.setServiceTypeId(companyServiceTypeDict.getDictId());
        entity.setServiceTypeName(companyServiceTypeDict.getName());

        entity.insert();
        return entity;
    }

    /**
     * 编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public CompanyServiceType editData(CompanyServiceTypeMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdEdit.class);
        Dict companyServiceTypeDict = baseDictService.getById(vo.getServiceTypeId());
        ObjectUtils.isNullThrow(companyServiceTypeDict, "产业标记不存在");

        boolean existsCompanyIdAndServiceTypeId = this.isExistsCompanyIdAndServiceTypeId(vo.getCompanyId(), vo.getServiceTypeId(), vo.getCompanyServiceTypeId());
        BooleanUtils.isTrueThrow(existsCompanyIdAndServiceTypeId, "该产业标记已经存在");

        //账值新的对象
        CompanyServiceType entity = this.getById(vo.getCompanyServiceTypeId());
        ObjectUtils.isNullThrow(entity, "数据不存在");

        entity.setCompanyId(vo.getCompanyId());
        entity.setServiceTypeId(vo.getServiceTypeId());
        entity.setServiceTypeId(companyServiceTypeDict.getDictId());
        entity.setServiceTypeName(companyServiceTypeDict.getName());

        entity.updateById();
        return entity;
    }

    /**
     * 获取指定公司,指定产来标记.数据
     *
     * @param companyId     公司ID
     * @param serviceTypeId 产业标记ID
     */
    @Transactional(rollbackFor = Exception.class)
    public CompanyServiceType getByCompanyIdAndServiceTypeId(
            @NonNull String companyId,
            @NonNull String serviceTypeId
    ) {
        CompanyServiceType companyServiceType = this.lambdaQuery()
                .eq(CompanyServiceType::getCompanyId, companyId)
                .eq(CompanyServiceType::getServiceTypeId, serviceTypeId)
                .last(LocalConstant.LIMIT_1)
                .one();
        return companyServiceType;
    }

    /**
     * 获取公司所有的产业标记
     *
     * @param companyId 公司ID
     */
    public List<CompanyServiceType> getByCompanyIdAndServiceTypeId(
            @NonNull String companyId
    ) {
        List<CompanyServiceType> companyServiceTypeList = this.lambdaQuery()
                .eq(CompanyServiceType::getCompanyId, companyId)
                .list();
        return companyServiceTypeList;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean isExistsCompanyIdAndServiceTypeId(
            String companyId,
            String serviceTypeId,
            String excludeId
    ) {
        Long count = this.lambdaQuery()
                .eq(CompanyServiceType::getCompanyId, companyId)
                .eq(CompanyServiceType::getServiceTypeId, serviceTypeId)
                .ne(StringUtils.isNotEmpty(excludeId), CompanyServiceType::getCompanyServiceTypeId, excludeId)
                .count();
        return count > 0;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String companyServiceTypeId) {
        CompanyServiceType data = this.getById(companyServiceTypeId);
        if (data == null) {
            return;
        }
        data.deleteById();
    }

    /**
     * 获取原始数据详情
     */
    public CompanyServiceTypeMapstruct.CompanyServiceTypeVo getDetailVo(String companyServiceTypeId) {
        CompanyServiceType entity = this.getById(companyServiceTypeId);
        CompanyServiceTypeMapstruct.CompanyServiceTypeVo vo = CompanyServiceTypeMapstruct.INSTANCE.toCompanyServiceTypeVo(entity);
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByCompanyId(String companyId) {
        this.lambdaUpdate().eq(CompanyServiceType::getCompanyId,companyId).remove();
    }
}